package com.zhiyou100.controller;

import cn.hutool.core.util.StrUtil;
import com.sun.corba.se.spi.orbutil.threadpool.Work;
import com.zhiyou100.entity.User;
import com.zhiyou100.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * work 控制层
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-10-19 23:01
 */
@RestController
@RequestMapping("/worker")
@Slf4j
public class Test04 {
    @Autowired
    private IUserService userService;

    @GetMapping("/list")
    public List<User> listWork() {
        return userService.list();
    }

    @GetMapping("/select/{id}")
    public User selectOneById(@PathVariable("id") Integer id) {
        log.error("id = {}", id);
        return userService.getById(id);
    }

    @PostMapping("/add")
    public String addOne(User user) {
        log.error(" user = {}", user);
        userService.save(user);
        return StrUtil.format(" 添加 {} 成功！", user);
    }

    @PutMapping("/update")
    public String update(User user) {
        log.error("user = {}", user);
        userService.saveOrUpdate(user);
        return StrUtil.format(" 修改 {} 成功！ " + user);
    }

    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        log.error(" id = {}", id);
        userService.removeById(id);
        return StrUtil.format("删除 {} 成功！", id);
    }


}
